package com.hzjh.base.style.view.actionbar

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.hzjh.base.style.R
import com.hzjh.base.style.util.StatusBarUtils


class AndlangBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var mStatusBar:View
    private var rlTop:RelativeLayout
    private var langTvTitle:TextView
    private var langTopLine:View
    private var langIvBack:ImageView
    private var langTvClose:TextView
    private var langTvRight:TextView
    init {
        LayoutInflater.from(context).inflate(R.layout.lang_common_top, this, true)

        mStatusBar=findViewById(R.id.m_statusBar)
        rlTop=findViewById(R.id.rl_top)
        langTvTitle=findViewById(R.id.lang_tv_title)
        langTopLine=findViewById(R.id.lang_top_line)
        langIvBack=findViewById(R.id.lang_iv_back)
        langTvClose=findViewById(R.id.lang_tv_close)
        langTvRight=findViewById(R.id.lang_tv_right)
        langIvBack.setOnClickListener{
            //默认点击事件
            val activity=context as Activity//那个活动转为当前活动
            activity.setResult(Activity.RESULT_OK)
            activity.finish()//关闭这个活动
        }

        initCustomAttrs(context, attrs)
    }

    private fun initCustomAttrs(context: Context,
                        attrs: AttributeSet? = null){
        //获取自定义属性。
        val attrs = context.obtainStyledAttributes(attrs, R.styleable.AndlangBar)

        //状态栏背景颜色
        val statusBarColor=attrs.getColor(R.styleable.AndlangBar_status_backgroud_color,resources.getColor(R.color.coreWhite))
        //状态栏是否显示
        val statusVisibility=attrs.getInt(R.styleable.AndlangBar_status_visibility,View.VISIBLE)
        //状态栏字体颜色黑或白
        val statusTextDark=attrs.getBoolean(R.styleable.AndlangBar_status_text_dark,true)

        //标题栏底部线的颜色
        val barLineColor=attrs.getColor(R.styleable.AndlangBar_bar_line_color,resources.getColor(R.color.coreLine))
        //标题栏背景色
        val titleBackground=attrs.getColor(R.styleable.AndlangBar_bar_background,resources.getColor(R.color.coreWhite))
        //标题栏标题字体颜色
        val titleTextColor=attrs.getColor(R.styleable.AndlangBar_bar_title_color,resources.getColor(R.color.coreBlack))
        //标题栏标题字体大小
        val titleTextSize=attrs.getDimension(R.styleable.AndlangBar_bar_title_size,resources.getDimension(R.dimen.core_title_size))
        //标题栏标题文案
        val barTitle=attrs.getString(R.styleable.AndlangBar_bar_title)
        //标题栏是否显示
        val barVisibility=attrs.getInt(R.styleable.AndlangBar_bar_visibility,View.VISIBLE)

        //返回键按钮
        val leftBackIcon=attrs.getResourceId(R.styleable.AndlangBar_bar_back_icon,R.mipmap.core_back_black)

        //设置状态栏
        setStatusBar(statusBarColor)
        setStatusTextDark(statusTextDark)
        mStatusBar.visibility=statusVisibility

        //设置标题栏
        rlTop.setBackgroundColor(titleBackground)
        rlTop.visibility= barVisibility
        langTvTitle.setTextColor(titleTextColor)
        langTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleTextSize)//字体大小适配
        langTvTitle.text=barTitle
        langTopLine.setBackgroundColor(barLineColor)
        langTvClose.setTextColor(titleTextColor)
        langTvRight.setTextColor(titleTextColor)

        langIvBack.setImageResource(leftBackIcon)
    }

    //设置点击返回按钮事件
    fun clickBackBtn(listener:View.OnClickListener){
        langIvBack.setOnClickListener(listener)
    }

    fun setTitle(title:String){
        langTvTitle.text=title
    }

    //flag 是否设置status状态栏 colorId状态栏颜色
    private fun setStatusBar(colorId: Int) {
        val activity=context as Activity//那个活动转为当前活动
        mStatusBar.visibility = VISIBLE
        val layoutParams = mStatusBar.layoutParams
        layoutParams.height = StatusBarUtils.getStatusHeight(activity)
        mStatusBar.layoutParams = layoutParams
        mStatusBar.setBackgroundColor(colorId)
    }
    /**
     *  ifDark
     *  true-状态栏显示黑色字体
     *  false-状态栏显示白色字体
     */
    private fun setStatusTextDark(ifDark: Boolean?=true){
        val activity=context as Activity//那个活动转为当前活动
        StatusBarUtils.translateStatusBar(activity)
        StatusBarUtils.setTextColorStatusBar(activity, ifDark!!)
    }

    fun setStatusBarVisibility(visibility:Int){
        mStatusBar.visibility=visibility
    }
    fun setTitleBarVisibility(visibility:Int){
        rlTop.visibility= visibility
    }

    fun setBackBtnVisibility(visibility:Int){
        langIvBack.visibility= visibility
    }
}